// controller/user.js
'use strict';
const Controller = require('egg').Controller;
class UserController extends Controller {
    async login() {
        const { ctx, app } = this;
        const { phone, password } = ctx.request.body;
        const userInfo = await ctx.service.dlqlogin.getUserByName(phone);
        if (!userInfo || !userInfo.id) {
            ctx.body = {
                code: 500,
                msg: '账号不存在',
                success: false,
                data: null,
            };
            return;
        }
        if (userInfo && password !== userInfo.password) {
            ctx.body = {
                code: 500,
                msg: '账号密码错误',
                success: false,
                data: null,
            };
            return;
        }
        // token 生成
        const token = app.jwt.sign(
            {
                id: userInfo.id,
                phone: userInfo.phone,
                exp: Math.floor(Date.now() / 1000) + 24 * 60 * 60, // token 有效期为 24 小时
            },
            app.config.jwt.secret
        );
        ctx.body = {
            code: 200,
            message: '登录成功',
            success: true,
            data: {
                token,
            },
        };
    }
    //获取个人信息
    async getUserInfo() {
        const { ctx, app } = this;
        const token = ctx.request.header.authorization;
        // console.log(token)
        const decode = await app.jwt.verify(token, app.config.jwt.secret);
        // 通过 token 带过来的 phone 获取到个人信息
        const userInfo = await ctx.service.dlqlogin.getUserByName(decode.phone);
        const defaultAvatar =
            'https://profile.csdnimg.cn/4/2/E/3_weixin_38992765';
        ctx.body = {
            code: 200,
            msg: '请求成功',
            data: {
                id: userInfo.id,
                phone: userInfo.phone,
                signature: userInfo.signature || '',
                avatar: userInfo.avatar || defaultAvatar,
                nickname: userInfo.nickname || '不知名先生',
                praise: userInfo.praise,
                password: userInfo.password
            },
        };
    }

    async register() {
        const { ctx } = this;
        const { phone, password, nickname } = ctx.request.body;
        const userInfo = await ctx.service.dlqlogin.getUserByName(phone);
        // 判断是否已经存在
        if (userInfo && userInfo.id) {
            ctx.body = {
                code: 500,
                msg: '账户名已被注册，请重新输入',
                data: null,
                success: false,
            };
            return;
        }
        const defaultAvatar =
            'https://profile.csdnimg.cn/4/2/E/3_weixin_38992765';
        const result = await ctx.service.dlqlogin.register({
            phone,
            password,
            nickname,
            ctime: new Date().getTime(),
            signature: '暂无个人签名',
            avatar: defaultAvatar,
        });
        if (result) {
            ctx.body = {
                code: 200,
                msg: '注册成功',
                data: null,
                success: true,
            };
        } else {
            ctx.body = {
                code: 500,
                msg: '注册失败',
                data: null,
                success: false,
            };
        }
    }
}
module.exports = UserController;